package org.wlw.teacher.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.wlw.teacher.models.Department;
import org.wlw.teacher.models.Pager;
import org.wlw.teacher.service.IDepartmentService;
import org.wlw.teacher.utils.Contrants;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/admin/dep")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    @GetMapping("/back/deps")
    public String list(Model model) {
        List<Department> deps = departmentService.list(null);
        for (Department dep : deps) {
            System.out.println(Contrants.DEP_TYPE.get(dep.getType()));
            dep.setTypeDesc(Contrants.DEP_TYPE.get(dep.getType()));
        }
        model.addAttribute("deps", deps);
        return "dep/list";
    }

    @GetMapping("/deps")
    public String find(Model model, Department searchObj) {
        Pager<Department> deps = departmentService.find(searchObj);
        for (Department dep : deps.getData()) {
            dep.setTypeDesc(Contrants.DEP_TYPE.get(dep.getType()));
        }
        model.addAttribute("datas", deps);
        model.addAttribute("searchObj", searchObj);
        model.addAttribute("types", Contrants.DEP_TYPE);
        return "dep/list";
    }

    @GetMapping("/delete_back")
    public String delete_back(Model model, HttpServletRequest request) {
        int id = Integer.parseInt(request.getParameter("id"));
        departmentService.delete(id);
        return "redirect:/admin/dep/deps";
    }

    /*
     * 传递参数的第二种方式
     * */
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable int id, Model model) {
        departmentService.delete(id);
        return "redirect:/admin/dep/deps";
    }

    @GetMapping("/add")
    public String add(Model model) {
        model.addAttribute("types", Contrants.DEP_TYPE);
        return "dep/add";
    }

    @PostMapping("/add")
    public String add(Department department) {
        departmentService.add(department);
        return "redirect:/admin/dep/deps";
    }

    @GetMapping("/update/{id}")
    public String update(@PathVariable int id, Model model) {
        Department dep = departmentService.load(id);
        model.addAttribute("dep", dep);
        model.addAttribute("types", Contrants.DEP_TYPE);
        return "dep/update";
    }

    @PostMapping("/update/{id}")
    public String update(@PathVariable int id, Department department) {
        Department dep = departmentService.load(id);
        dep.setPhone(department.getPhone());
        dep.setType(department.getType());
        dep.setName(department.getName());
        departmentService.update(dep);
        return "redirect:/admin/dep/deps";
    }
}
